package com.xbongbong.saas.analytical.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.help.CommonHelp;
import com.xbongbong.paas.help.subform.TransferSubFormHelper;
import com.xbongbong.paas.pojo.dto.ValidateDataDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.WarehouseErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.analytical.SaasValidateAnalyticalService;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.WarehouseEnum;
import com.xbongbong.saas.help.SaasProductHelp;
import com.xbongbong.saas.service.toolbox.help.UserTeamHelp;
import com.xbongbong.saas.toolbox.help.FastJsonHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author 吴峰
 * @date 2021/07/15 17:15
 */
@Service("wareHouseAnalyticalServiceImpl")
public class WareHouseValidateAnalyticalServiceImpl implements SaasValidateAnalyticalService {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(WareHouseValidateAnalyticalServiceImpl.class);
    
    @Resource
    private UserTeamHelp userTeamHelp;
    @Resource
    private CommonHelp commonHelp;
    @Resource
    private TransferSubFormHelper transferSubFormHelper;
    @Resource
    private SaasProductHelp saasProductHelp;

    @Override
    public List<Integer> type() {
        return Arrays.asList(XbbRefTypeEnum.WAREHOUSE.getCode());
    }

    @Override
    public void analyticalData(ValidateDataDTO validateDataDTO) throws XbbException {
        beforeSave(validateDataDTO);
    }
    
    public void beforeSave(ValidateDataDTO validateDataDTO) throws XbbException {
        try {
            int warehouseNameMax = 100;
            JSONObject data = validateDataDTO.getData();
            String warehouseName = FastJsonHelper.getStringFromFormData(data, WarehouseEnum.WAREHOUSE_NAME.getAttr());
            if ((StringUtil.isEmpty(warehouseName))) {
                throw new XbbException(WarehouseErrorCodeEnum.API_ERROR_244013);
            } else if (warehouseName.length() > warehouseNameMax) {
                throw new XbbException(WarehouseErrorCodeEnum.API_ERROR_244015);
            }
            warehouseName = StringUtil.replaceBlank(warehouseName);
            data.put(WarehouseEnum.WAREHOUSE_NAME.getAttr(), warehouseName);
            validateDataDTO.setData(data);
        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOGGER.error("wareHouseAnalyticalServiceImpl.beforeSave error", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
    }

    
}
